package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.client.enums.TriggerStrategy;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.VideoMonitorModelAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@AttrHandler(VideoMonitorModelAttrHandler.class)
public class VideoMonitorModelAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        // 处理框架产品与子产品的型号属性联动

        this.modelAttrLinkHandle(attr, inst, data);

        // 处理结算价、销售价、产品公允价
        if(!TriggerStrategy.LOAD_VALUE.equals(data.getTriggerStrategy())){
            this.dealPrice(attr, inst, data);
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    private void modelAttrLinkHandle(KiteAttr attr, KiteInst inst, AttrData data) {
        String offerInstId = querySceneInstService.getSceneInst(data.getSceneInstId()).getRootInstId();
        List<KiteProdInst> prodInstList = querySceneInstService.listProdInstsFromGiftOfferInst(data.getCustId(),
            offerInstId);
        prodInstList.forEach(prodInst -> {
            if (KiteStringUtils.equals(inst.getInstId(), prodInst.getInstId()) || Objects
                .isNull(prodInst.getAttr("200002772"))) {
                return;
            }
            if (!KiteStringUtils.equalsAny(prodInst.getSpecId(), "63249372", "63249373", "63249556")) {
                return;
            }
            operateInstAttrService.setAttrValue(prodInst, "200002772", attr.getValue(),
                data.getSceneInstId(), null);
        });
    }

    private void dealPrice(KiteAttr attr, KiteInst inst, AttrData data) {
        // 产品公允价
        KiteAttr commonPrice = inst.getAttr("200004115");
        // 销售价
        KiteAttr salesPrice = inst.getAttr("200004107");
        // 结算价
        KiteAttr settlePrice = inst.getAttr("200004108");
        if (Objects.nonNull(commonPrice)) {
            String attrValueSpec = AttrValueCache.getAttrValueByAttrValueCode(commonPrice.getAttrId(), attr.getValue());
            operateInstAttrService.setAttrValue(inst, commonPrice.getFieldName(), attrValueSpec, data.getSceneInstId(), null);
        }
        if (Objects.nonNull(salesPrice)) {
            String attrValueSpec = AttrValueCache.getAttrValueByAttrValueCode(salesPrice.getAttrId(), attr.getValue());
            operateInstAttrService.setAttrValue(inst, salesPrice.getFieldName(), attrValueSpec, data.getSceneInstId(), null);
        }
        if (Objects.nonNull(settlePrice)) {
            String attrValueSpec = AttrValueCache.getAttrValueByAttrValueCode(settlePrice.getAttrId(), attr.getValue());
            operateInstAttrService.setAttrValue(inst, settlePrice.getFieldName(), attrValueSpec, data.getSceneInstId(), null);
        }
    }


}
